﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using OT.Data;
using OT.Areas.Admin.Models;
using PagedList;
namespace OT.Areas.Admin.Controllers
{
    public class SubjectController : Controller
    {
        #region Declaration
        readonly OTEntitiesme OTDb = new OTEntitiesme();
        int LanguageID = Utilities.GetLanguageID();
        #endregion

        #region Index
        public ActionResult Index()
        {
            return View();
        }
        [HttpPost]
        public JsonResult Index(int pageNumber, int pageSize, string searchString, int pgindex)
        {
            List<Subjects> sub = (from c in OTDb.Subjects
                                  join sc in OTDb.Categories
                                  on c.CategoryID equals sc.CategoryID into output
                                  from j in output.DefaultIfEmpty()
                                  where c.LanguageID == LanguageID
                                  select new Subjects
                                  {
                                      SubjectID = c.SubjectID,
                                      SubjectName = c.SubjectName,
                                      Description = c.Description,
                                      Status = (bool)c.Status,
                                      CategoryName = j.CategoryName,
                                      CreatedDate = (c.CreatedDate.HasValue) ? c.CreatedDate.Value : DateTime.MinValue,
                                      UpdatedDate = (c.UpdatedDate.HasValue) ? c.UpdatedDate.Value : DateTime.MinValue
                                  }).OrderBy(o => o.SubjectName).ToList<Subjects>(); ;
            if (!string.IsNullOrEmpty(searchString))
                sub = sub.Where(r => r.SubjectName.ToLower().Contains(searchString.ToLower())).ToList();
            int recount = sub.Count;
            sub = sub.Skip<Subjects>(pgindex * (pageNumber - 1))
                .Take<Subjects>(pgindex).ToList<Subjects>();
            return Json(new
            {
                PageNumber = pageNumber,
                PageSize = pageSize,
                PgIndex = pgindex,
                Data = sub,
                RecordCount = recount
            });
        }
        #endregion

        #region Create
        public ActionResult Create()
        {
            Subjects topic = null;
            try
            {
                topic = new Subjects();
                var sub = OTDb.Categories.Where(c=>c.Status==true).ToList();
                topic.categorys = sub;
            }
            catch (Exception ex)
            {
                Utilities.InsertErrorLog(ex);
            }
            return PartialView(topic);
        }
        [HttpPost]
        public string Create(String SubjectName, String Description, Boolean Status, int CategoryID)
        {
            string result = "";
            Subject sub = null;
            try
            {
                if (ModelState.IsValid)
                {
                    sub = new Subject();
                    sub.SubjectName = SubjectName;
                    sub.Description = Description;
                    sub.Status = Status;
                    sub.LanguageID = LanguageID;
                    sub.CategoryID = CategoryID;
                    sub.CreatedDate = DateTime.Now;
                    sub.UpdatedDate = DateTime.Now;
                    OTDb.Subjects.Add(sub);
                    OTDb.SaveChanges();
                    result = "Sucess";
                }
                else
                {
                    var Citys = OTDb.Subjects.ToList();
                    result = "NotValid";
                }
            }
            catch (Exception ex)
            {
                Utilities.InsertErrorLog(ex);
            }
            return result;
        }
        #endregion

        #region Edit
        public ActionResult Edit(int SubjectID)
        {
            Subjects sb = null;
            try
            {
                var categry = OTDb.Subjects.Where(r => r.SubjectID == SubjectID ).SingleOrDefault();
                if (categry != null)
                {
                    sb = new Subjects();
                    sb.SubjectID = categry.SubjectID;
                    sb.SubjectName = categry.SubjectName;
                    sb.SubjectID = categry.SubjectID;
                    sb.Description = categry.Description;
                    sb.CategoryID = categry.CategoryID;
                    sb.Status =(bool) categry.Status;
                    sb.LanguageID = LanguageID;
                    var user = OTDb.Categories.ToList();
                    sb.categorys = user;
                }
            }
            catch (Exception ex)
            {
                Utilities.InsertErrorLog(ex);
            }
            return PartialView(sb);
        }

        [HttpPost]
        public string Edit(int SubjectID, String SubjectName, String Description, Boolean Status, int CategoryID)
        {
            string result = "";
            try
            {
                if (ModelState.IsValid)
                {
                    var sb = OTDb.Subjects.Where(r => r.SubjectID == SubjectID).SingleOrDefault();
                    if (sb != null)
                    {
                        sb.SubjectID = SubjectID;
                        sb.SubjectName = SubjectName;
                        sb.SubjectID = SubjectID;
                        sb.Description = Description;
                        sb.CategoryID = CategoryID;
                        sb.Status = Status;
                        sb.LanguageID = LanguageID;
                        sb.UpdatedDate = DateTime.Now;
                        OTDb.SaveChanges();
                        result = "Sucess";
                    }
                }
                else
                {
                    result = "NotValid";
                }
            }
            catch (Exception ex)
            {
                Utilities.InsertErrorLog(ex);
            }
            return result;
        }
        #endregion

        #region Delete
        public ActionResult Delete(int SubjectID)
        {
            Subjects sb1 = null; 
            try
            {
                var categry = OTDb.Subjects.Where(r => r.SubjectID == SubjectID).SingleOrDefault();
                if (categry != null)
                {
                    sb1 = new Subjects();
                    sb1.SubjectName = categry.SubjectName;
                    sb1.Description = categry.Description;
                    sb1.CategoryID = categry.UserID;
                    sb1.LanguageID = LanguageID;
                    sb1.Status =(bool) categry.Status;
                }
            }
            catch (Exception ex)
            {
                Utilities.InsertErrorLog(ex);
            }
            return PartialView(sb1);
        }

        [HttpPost]
        public string Delete(int SubjectID, string IsDelete)
        {
            string result = "";
            try
            {
                var cat = OTDb.Subjects.Where(r => r.SubjectID  == SubjectID).SingleOrDefault();
                if (cat != null)
                {
                    OTDb.Subjects.Remove(cat);
                    OTDb.SaveChanges();
                    result = "Success";
                }
            }
            catch (Exception ex)
            {
                Utilities.InsertErrorLog(ex);
            }
            return result;
        }
        #endregion
    }
}
